download.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from __future__ import absolute_import
  2. import logging
  3. import os
  4. from pip._internal.cli import cmdoptions
  5. from pip._internal.cli.cmdoptions import make_target_python
  6. from pip._internal.cli.req_command import RequirementCommand, with_cleanup
  7. from pip._internal.cli.status_codes import SUCCESS
  8. from pip._internal.req.req_tracker import get_requirement_tracker
  9. from pip._internal.utils.misc import ensure_dir, normalize_path, write_output
  10. from pip._internal.utils.temp_dir import TempDirectory
  11. from pip._internal.utils.typing import MYPY_CHECK_RUNNING
  12. if MYPY_CHECK_RUNNING:
  13. from optparse import Values
  14. from typing import List
  15. logger = logging.getLogger(__name__)
  16. class DownloadCommand(RequirementCommand):
  17. """
  18. Download packages from:
  19. - PyPI (and other indexes) using requirement specifiers.
  20. - VCS project urls.
  21. - Local project directories.
  22. - Local or remote source archives.
  23. pip also supports downloading from "requirements files", which provide
  24. an easy way to specify a whole environment to be downloaded.
  25. """
  26. usage = """
  27. %prog [options] <requirement specifier> [package-index-options] ...
  28. %prog [options] -r <requirements file> [package-index-options] ...
  29. %prog [options] <vcs project url> ...
  30. %prog [options] <local project path> ...
  31. %prog [options] <archive url/path> ..."""
  32. def add_options(self):
  33. # type: () -> None
  34. self.cmd_opts.add_option(cmdoptions.constraints())
  35. self.cmd_opts.add_option(cmdoptions.requirements())
  36. self.cmd_opts.add_option(cmdoptions.build_dir())
  37. self.cmd_opts.add_option(cmdoptions.no_deps())
  38. self.cmd_opts.add_option(cmdoptions.global_options())
  39. self.cmd_opts.add_option(cmdoptions.no_binary())
  40. self.cmd_opts.add_option(cmdoptions.only_binary())
  41. self.cmd_opts.add_option(cmdoptions.prefer_binary())
  42. self.cmd_opts.add_option(cmdoptions.src())
  43. self.cmd_opts.add_option(cmdoptions.pre())
  44. self.cmd_opts.add_option(cmdoptions.require_hashes())
  45. self.cmd_opts.add_option(cmdoptions.progress_bar())
  46. self.cmd_opts.add_option(cmdoptions.no_build_isolation())
  47. self.cmd_opts.add_option(cmdoptions.use_pep517())
  48. self.cmd_opts.add_option(cmdoptions.no_use_pep517())
  49. self.cmd_opts.add_option(
  50. '-d', '--dest', '--destination-dir', '--destination-directory',
  51. dest='download_dir',
  52. metavar='dir',
  53. default=os.curdir,
  54. help=("Download packages into <dir>."),
  55. )
  56. cmdoptions.add_target_python_options(self.cmd_opts)
  57. index_opts = cmdoptions.make_option_group(
  58. cmdoptions.index_group,
  59. self.parser,
  60. )
  61. self.parser.insert_option_group(0, index_opts)
  62. self.parser.insert_option_group(0, self.cmd_opts)
  63. @with_cleanup
  64. def run(self, options, args):
  65. # type: (Values, List[str]) -> int
  66. options.ignore_installed = True
  67. # editable doesn't really make sense for `pip download`, but the bowels
  68. # of the RequirementSet code require that property.
  69. options.editables = []
  70. cmdoptions.check_dist_restriction(options)
  71. options.download_dir = normalize_path(options.download_dir)
  72. ensure_dir(options.download_dir)
  73. session = self.get_default_session(options)
  74. target_python = make_target_python(options)
  75. finder = self._build_package_finder(
  76. options=options,
  77. session=session,
  78. target_python=target_python,
  79. )
  80. build_delete = (not (options.no_clean or options.build_dir))
  81. req_tracker = self.enter_context(get_requirement_tracker())
  82. directory = TempDirectory(
  83. options.build_dir,
  84. delete=build_delete,
  85. kind="download",
  86. globally_managed=True,
  87. )
  88. reqs = self.get_requirements(args, options, finder, session)
  89. preparer = self.make_requirement_preparer(
  90. temp_build_dir=directory,
  91. options=options,
  92. req_tracker=req_tracker,
  93. session=session,
  94. finder=finder,
  95. download_dir=options.download_dir,
  96. use_user_site=False,
  97. )
  98. resolver = self.make_resolver(
  99. preparer=preparer,
  100. finder=finder,
  101. options=options,
  102. py_version_info=options.python_version,
  103. )
  104. self.trace_basic_info(finder)
  105. requirement_set = resolver.resolve(
  106. reqs, check_supported_wheels=True
  107. )
  108. downloaded = ' '.join([req.name # type: ignore
  109. for req in requirement_set.requirements.values()
  110. if req.successfully_downloaded])
  111. if downloaded:
  112. write_output('Successfully downloaded %s', downloaded)
  113. return SUCCESS